define("admin/page/permission/group_list", [
    "jquery",
    "vue",
    "vue-resource",
    "sweetalert",
    "toastr"
], function($, Vue, VueHttp, swal, toastr){
    Vue.use(VueHttp);
    var vm = new Vue({
        el : '#permission_group',
        delimiters: ['${', '}'],

        data: {
            pageData: {},
            status: 0,
            search: '',
            edit_permission_group_url: '/admin/permission/group_edit',
            del_permission_group_url: '/admin/permission/group_del'
        },
        
        methods: {
            turnPage: function(index){
                if (index != this.pageData.current_page && index != 0 && index <= this.pageData.last_page) {
                    Vue.set(this.pageData, 'current_page', index);
                }
            },

            page_jump: function(){
                window.location.href = '/admin/permission/group?page=' + this.pageData.current_page + '&status=' + this.status + '&search=' + encodeURI(encodeURI(this.search));
            },

            search_jump: function(){
                window.location.href = '/admin/permission/group?page=1&status=' + this.status + '&search=' + encodeURI(encodeURI(this.search));
            },

            // 获取url中的参数
            getUrlParam: function(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
                var r = window.location.search.substr(1).match(reg);  //匹配目标参数
                if (r != null) return unescape(r[2]); return null; //返回参数值
            },
            delRecord : function(id) {
                if (! id) return ;
                var self = this;
                swal({
                    title: '确认删除',
                    text: '删除后，将无法恢复！',
                    //type: 'warning',
                    showCancelButton: true,
                    confirmButtonText: '删除',
                    cancelButtonText: '取消',
                })
                    .then(function(result) {
                        if (result.value) {
                            self.$http.get(self.del_permission_group_url, {params:{id: id}})
                                .then(function(response){
                                    return response.json();
                                })
                                .then(function (json) {
                                    if (json.ret == 0) {
                                        toastr.success(json.msg);
                                        setTimeout(function(){
                                            window.location.reload();
                                        },3000)
                                    } else {
                                        toastr.warning(json.msg);
                                    }
                                });
                        }
                    });
            },
        },

        mounted: function(){
            Vue.set(this, 'pageData', PAGE.meta);

            this.status = this.getUrlParam('status') ? this.getUrlParam('status') : 0;
            this.search = this.getUrlParam('search') ? decodeURI(this.getUrlParam('search')) : '';

            this.unwatch_current_page = this.$watch('pageData.current_page', this.page_jump);
            this.unwatch_status = this.$watch('status', this.search_jump);
        },
    });
});